package com.neuedu.controller;

import com.neuedu.bean.Cart;
import com.neuedu.bean.Orders;
import com.neuedu.common.R;
import com.neuedu.service.CartService;
import com.neuedu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 1. 将该类转换控制器类，并设置该中的方法返回值全部都是json数据
//@Controller
//@ResponseBody
@RestController
// 2. 设置该模块的请求路径，如/user
@RequestMapping("/orders")
// 3. 解决跨域
@CrossOrigin
public class OrdersController {

    @Autowired
    private OrdersService ordersService;


    @PostMapping("/createOrders")
    public R createOrders(Orders orders){
        try {
            int ordersId = ordersService.createOrders(orders);
            return R.ok().data(ordersId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/getOrdersById/{ordersId}")
    public R getOrdersById(@PathVariable("ordersId") Integer ordersId){
        try {
            Orders orders = ordersService.getOrdersById(ordersId);
            return R.ok().data(orders);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/listOrdersByUserId/{userId}")
    public R listOrdersByUserId(@PathVariable("userId") String userId){
        try {
            List<Orders> ordersList = ordersService.listOrdersByUserId(userId);
            return R.ok().data(ordersList);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/updateOrders/{ordersId}")
    public R updateOrders(@PathVariable("ordersId") Integer ordersId){
        try {
            int i = ordersService.updateOrders(ordersId);
            return R.ok().data(i);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
